package com.cloudcreativity.blyg.ui.account;

import android.app.Activity;
import android.databinding.ObservableField;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.cloudcreativity.blyg.R;
import com.cloudcreativity.blyg.base.BaseBindingRecyclerViewAdapter;
import com.cloudcreativity.blyg.base.BaseDialogImpl;
import com.cloudcreativity.blyg.base.BaseModel;
import com.cloudcreativity.blyg.databinding.ActivitySellStatisticsBinding;
import com.cloudcreativity.blyg.databinding.ItemLayoutSellStatisticsBinding;
import com.cloudcreativity.blyg.entity.SellPage;
import com.cloudcreativity.blyg.utils.DefaultObserver;
import com.cloudcreativity.blyg.utils.HttpUtils;
import com.cloudcreativity.blyg.utils.MyDatePicker;
import com.cloudcreativity.blyg.utils.SPUtils;
import com.cloudcreativity.blyg.utils.ToastUtils;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import java.util.Calendar;
import java.util.Locale;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class SellStatisticsModel extends BaseModel<Activity, ActivitySellStatisticsBinding> {

    public BaseBindingRecyclerViewAdapter<SellPage.Statistics, ItemLayoutSellStatisticsBinding> adapter;
    public ObservableField<String> currentTitle = new ObservableField<>();
    private int pageNum;

    private Integer currentYear;
    private Integer currentMonth;
    private Integer currentDay;

    SellStatisticsModel(Activity context, final ActivitySellStatisticsBinding binding, BaseDialogImpl baseDialog) {
        super(context, binding, baseDialog);

        adapter = new BaseBindingRecyclerViewAdapter<SellPage.Statistics, ItemLayoutSellStatisticsBinding>(context) {
            @Override
            protected int getLayoutResId(int viewType) {
                return R.layout.item_layout_sell_statistics;
            }

            @Override
            protected void onBindItem(ItemLayoutSellStatisticsBinding binding, SellPage.Statistics item, int position) {
                binding.setItem(item);
            }
        };
        binding.rcvGoods.setLayoutManager(new LinearLayoutManager(context,
                LinearLayoutManager.VERTICAL,
                false));
        //加上分割线
        DividerItemDecoration itemDecoration = new DividerItemDecoration(context,DividerItemDecoration.VERTICAL);
        itemDecoration.setDrawable(context.getResources().getDrawable(R.drawable.shape_item_divide));
        binding.rcvGoods.addItemDecoration(itemDecoration);

        Calendar instance = Calendar.getInstance(Locale.CHINESE);
        currentYear = instance.get(Calendar.YEAR);
        currentMonth = instance.get(Calendar.MONTH)+1;
        currentDay = instance.get(Calendar.DAY_OF_MONTH);

        currentTitle.set(currentYear+"-"+(currentMonth<10?("0"+currentMonth):currentMonth)
        +"-"+(currentDay<10?("0"+currentDay):currentDay));

        binding.refreshSell.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                pageNum = 1;
                loadData(currentYear,currentMonth,currentDay,pageNum);
            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                loadData(currentYear,currentMonth,currentDay,pageNum);
            }
        });

        binding.refreshSell.postDelayed(new Runnable() {
            @Override
            public void run() {
                binding.refreshSell.startRefresh();
            }
        },200);
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    public void onBack() {
        context.finish();
    }

    public void onDateClick(){
        MyDatePicker myDatePicker = new MyDatePicker(context);
        myDatePicker.setOnChooseListener(new MyDatePicker.OnChooseListener() {
            @Override
            public void onChoose(int year, int month, int day) {
                Calendar instance = Calendar.getInstance(Locale.CHINESE);
                Calendar i2 = (Calendar) instance.clone();
                i2.set(Calendar.YEAR,year);
                i2.set(Calendar.MONTH,month);
                i2.set(Calendar.DAY_OF_MONTH,day);

                if(i2.getTimeInMillis() > instance.getTimeInMillis()) {
                    ToastUtils.showShortToast(context,"日期不能大于今天");
                }else {
                    currentYear = year;
                    currentMonth = month + 1;
                    currentDay = day;
                    currentTitle.set(currentYear + "-" + (currentMonth < 10 ? ("0" + currentMonth) : currentMonth)
                            + "-"+(currentDay < 10 ? ("0" + currentDay) : currentDay));
                    pageNum = 1;
                    binding.refreshSell.startRefresh();
                }
            }
        });
        myDatePicker.show();
    }

    private void loadData(int year,int month,int day,final int page){
        int shopId = SPUtils.get().getUser().getShopId();
        HttpUtils.getInstance().getStatistics(shopId,year,month,day,page,50)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DefaultObserver<SellPage>(getBaseDialog(),false) {
                    @Override
                    public void onSuccess(SellPage t) {
                        if(page==1){
                            binding.refreshSell.finishRefreshing();
                        }else{
                            binding.refreshSell.finishLoadmore();
                        }

                        if(t.getInfo().getRecords()==null|| t.getInfo().getRecords().isEmpty()){
                            //数据为空
                            if(page==1){
                                adapter.getItems().clear();
                                binding.noData.setVisibility(View.VISIBLE);
                                binding.rcvGoods.setVisibility(View.GONE);
                            }else{
                                binding.refreshSell.setEnableLoadmore(false);
                            }
                        }else{
                            //添加数据
                            if(page==1){
                                adapter.getItems().clear();
                                binding.noData.setVisibility(View.GONE);
                                binding.rcvGoods.setVisibility(View.VISIBLE);
                            }

                            adapter.getItems().addAll(t.getInfo().getRecords());

                            if(t.getInfo().getTotalpage()>page){
                                binding.refreshSell.setEnableLoadmore(true);
                                pageNum ++;
                            }else{
                                binding.refreshSell.setEnableLoadmore(false);
                            }
                        }
                    }

                    @Override
                    public void onFail(ExceptionReason msg) {
                        if(page==1){
                            binding.refreshSell.finishRefreshing();
                        }else{
                            binding.refreshSell.finishLoadmore();
                        }
                    }
                });
    }
}
